package com.anos.demoinserticon;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyActivity extends Activity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private TextView tvResult;
    private Button btnAdd;
    private Button btnAddIcon;
    private GridView gvIcon;

    private ArrayList<String> mStringIcons = new ArrayList<String>();
    private ArrayList<Bitmap> mBitmapIcons = new ArrayList<Bitmap>();

    private ListIconAdapter adapter;
    private String mText = "Text: ";

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tvResult = (TextView) findViewById(R.id.tvResult);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        gvIcon = (GridView) findViewById(R.id.gvListIcon);

        btnAdd.setOnClickListener(this);

        // setup list icon
        setupListIcon();

        float scalefactor = getResources().getDisplayMetrics().density * 45;
        int number = getWindowManager().getDefaultDisplay().getWidth();
        int columns = (int) ((float) number / (float) scalefactor);
        gvIcon.setNumColumns(columns);

        adapter = new ListIconAdapter(this, mBitmapIcons);
        gvIcon.setAdapter(adapter);
        gvIcon.setOnItemClickListener(this);
    }

    private void setupListIcon() {
        mBitmapIcons.clear();
        mStringIcons.clear();
        String[] arrIcon = getResources().getStringArray(R.array.arr_icon);
        final int count = arrIcon.length;
        for (int i = 0; i < count; i++) {
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.sms_01);
            mStringIcons.add(arrIcon[i]);
            mBitmapIcons.add(bm);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnAdd:

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final String textSelected = mStringIcons.get(position);
//        final Bitmap bitmapSelected = mBitmapIcons.get(position);

        mText = "Text " + position + " - " + textSelected;
        Spannable text = EmoticonsMgr.getSmiledText(this, mText);
        tvResult.setText(text);
    }


}
